import { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'

// 生成模拟的看板组
const mockGroups = [
  {
    id: '1',
    name: '默认分组',
    count: 4
  },
  {
    id: '2',
    name: '智能丁小姐',
    count: 1
  }
]

// 生成模拟的看板列表数据
const mockDashboards = [
  {
    id: '1',
    title: '示例看板1：销售分析',
    description: '销售数据分析和可视化',
    groupId: '1',
    thumbnail: '',
    charts: [
      {
        id: 'chart-1',
        title: '月度销售额',
        type: 'bar',
        options: {
          xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: [120, 200, 150, 80, 70, 110],
              type: 'bar'
            }
          ]
        }
      }
    ]
  },
  {
    id: '2',
    title: '示例看板2：成本与利润',
    description: '成本与利润分析',
    groupId: '1',
    thumbnail: '',
    charts: [
      {
        id: 'chart-2',
        title: '利润分析',
        type: 'line',
        options: {
          xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: [120, 130, 180, 210, 190, 220],
              type: 'line'
            }
          ]
        }
      }
    ]
  },
  {
    id: '3',
    title: '示例看板3：成本分析',
    description: '成本分析看板',
    groupId: '1',
    thumbnail: '',
    charts: [
      {
        id: 'chart-3',
        title: '成本构成',
        type: 'pie',
        options: {
          series: [
            {
              type: 'pie',
              radius: '50%',
              data: [
                { value: 335, name: '材料费' },
                { value: 310, name: '人工费' },
                { value: 234, name: '管理费' },
                { value: 135, name: '推广费' },
                { value: 1548, name: '其他费用' }
              ]
            }
          ]
        }
      }
    ]
  },
  {
    id: '4',
    title: '智能丁小姐',
    description: '智能助手看板',
    groupId: '2',
    thumbnail: '',
    charts: [
      {
        id: 'chart-4',
        title: '数据分析',
        type: 'bar',
        options: {
          xAxis: {
            type: 'category',
            data: ['产品A', '产品B', '产品C', '产品D', '产品E']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: [154, 123, 145, 168, 192],
              type: 'bar'
            }
          ]
        }
      }
    ]
  }
]

// 获取看板分组列表
const getGroups = {
  url: '/api/dashboard/groups',
  method: 'get',
  response: () => {
    return {
      code: 200,
      data: mockGroups,
      message: '获取分组成功'
    }
  }
}

// 获取看板列表
const getDashboards = {
  url: '/api/dashboard/list',
  method: 'get',
  response: ({ query }) => {
    const { groupId, keyword } = query
    
    let result = [...mockDashboards]
    
    if (groupId) {
      result = result.filter(item => item.groupId === groupId)
    }
    
    if (keyword) {
      result = result.filter(item => 
        item.title.includes(keyword) || 
        item.description.includes(keyword)
      )
    }
    
    return {
      code: 200,
      data: result,
      message: '获取看板列表成功'
    }
  }
}

// 获取看板详情
const getDashboardDetail = {
  url: '/api/dashboard/detail',
  method: 'get',
  response: ({ query }) => {
    const { id } = query
    const dashboard = mockDashboards.find(item => item.id === id)
    
    if (dashboard) {
      return {
        code: 200,
        data: dashboard,
        message: '获取看板详情成功'
      }
    } else {
      return {
        code: 404,
        data: null,
        message: '看板不存在'
      }
    }
  }
}

// 创建新看板
const createDashboard = {
  url: '/api/dashboard/create',
  method: 'post',
  response: ({ body }) => {
    const { title, groupId, description = '' } = body
    
    const newId = Mock.mock('@id')
    
    const newDashboard = {
      id: newId,
      title,
      description,
      groupId,
      thumbnail: '',
      charts: []
    }
    
    // 在实际应用中会将新看板添加到数据库
    
    return {
      code: 200,
      data: newDashboard,
      message: '创建看板成功'
    }
  }
}

// 保存看板数据
const saveDashboard = {
  url: '/api/dashboard/save',
  method: 'post',
  response: ({ body }) => {
    const { id, charts } = body
    const dashboard = mockDashboards.find(item => item.id === id)
    
    if (dashboard) {
      // 在实际应用中会更新数据库中的charts数据
      return {
        code: 200,
        data: {
          id,
          charts
        },
        message: '保存成功'
      }
    } else {
      return {
        code: 404,
        data: null,
        message: '看板不存在'
      }
    }
  }
}

// 创建看板分组
const createDashboardGroup = {
  url: '/api/dashboard/group/create',
  method: 'post',
  response: ({ body }) => {
    const { name } = body
    
    const newId = Mock.mock('@id')
    
    const newGroup = {
      id: newId,
      name,
      count: 0
    }
    
    // 在实际应用中会将新分组添加到数据库
    
    return {
      code: 200,
      data: newGroup,
      message: '创建分组成功'
    }
  }
}

export default [
  getGroups,
  getDashboards,
  getDashboardDetail,
  createDashboard,
  saveDashboard,
  createDashboardGroup
] as MockMethod[] 